/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.star.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.ConfigUtils;
import com.jeesite.modules.sys.utils.DictUtils;
import com.star.modules.sys.enums.DictEnum;
import com.star.modules.sys.util.Html2Text;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.star.modules.sys.entity.ShInfo;
import com.star.modules.sys.service.ShInfoService;

import java.util.List;

/**
 * 信息发布(关于我们、资讯百科、新闻动态)Controller
 *
 * @author L
 * @version 2018-09-03
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/shInfo")
public class ShInfoController extends BaseController {


    @Autowired
    private ShInfoService shInfoService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public ShInfo get(String id, boolean isNewRecord) {
        return shInfoService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("sys:shInfo:view")
    @RequestMapping(value = {"list", ""})
    public String list(ShInfo shInfo, Model model) {
        model.addAttribute("shInfo", shInfo);
        return "modules/sys/shInfoList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("sys:shInfo:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<ShInfo> listData(ShInfo shInfo, HttpServletRequest request, HttpServletResponse response) {
        Page<ShInfo> page = shInfoService.findPage(new Page<ShInfo>(request, response), shInfo);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("sys:shInfo:view")
    @RequestMapping(value = "form")
    public String form(ShInfo shInfo, Model model) {
        model.addAttribute("shInfo", shInfo);
        List<DictData> dictDataList = DictUtils.getDictList(DictEnum.INFO_TYPE.getCode());
        List<DictData> dataList = ListUtils.newArrayList();
        for (DictData dict : dictDataList) {
            if (dict.getDictValue().equals(shInfo.getType().toString())) {
                for (DictData dictData : dictDataList) {
                    if (dictData.getParentCode().equals(dict.getId())) {
                        dataList.add(dictData);
                    }
                }
                break;
            }
        }
        model.addAttribute("dataList", dataList);
        return "modules/sys/shInfoForm";
    }

    /**
     * 保存信息发布(关于我们、资讯百科、新闻动态)
     */
    @RequiresPermissions("sys:shInfo:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated ShInfo shInfo) {
        if (StringUtils.isBlank(shInfo.getSource())) {//如果来源为空，给默认值
            shInfo.setSource(ConfigUtils.getConfig("info.default.source").getConfigValue());
        }
        if (StringUtils.isBlank(shInfo.getDescribe())) {//如果简介为空，提取默认字符
            String content = Html2Text.html2text(shInfo.getContent());
            int length = new Integer(ConfigUtils.getConfig("info.default.length").getConfigValue());
            shInfo.setDescribe(content.length() > length ? content.substring(0, length) : content);
        }
        shInfoService.save(shInfo);
        return renderResult(Global.TRUE, "保存信息发布(关于我们、资讯百科、新闻动态)成功！");
    }

    /**
     * 停用信息发布(关于我们、资讯百科、新闻动态)
     */
    @RequiresPermissions("sys:shInfo:edit")
    @RequestMapping(value = "disable")
    @ResponseBody
    public String disable(ShInfo shInfo) {
        shInfo.setStatus(ShInfo.STATUS_DISABLE);
        shInfoService.updateStatus(shInfo);
        return renderResult(Global.TRUE, "停用信息发布(关于我们、资讯百科、新闻动态)成功");
    }

    /**
     * 启用信息发布(关于我们、资讯百科、新闻动态)
     */
    @RequiresPermissions("sys:shInfo:edit")
    @RequestMapping(value = "enable")
    @ResponseBody
    public String enable(ShInfo shInfo) {
        shInfo.setStatus(ShInfo.STATUS_NORMAL);
        shInfoService.updateStatus(shInfo);
        return renderResult(Global.TRUE, "启用信息发布(关于我们、资讯百科、新闻动态)成功");
    }

    /**
     * 删除信息发布(关于我们、资讯百科、新闻动态)
     */
    @RequiresPermissions("sys:shInfo:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(ShInfo shInfo) {
        shInfoService.delete(shInfo);
        return renderResult(Global.TRUE, "删除信息发布(关于我们、资讯百科、新闻动态)成功！");
    }

    /**
     * 启用房源信息
     */
    @RequiresPermissions("sys:shHouse:edit")
    @RequestMapping(value = "ishot")
    @ResponseBody
    public String ishot(ShInfo shInfo) {
        shInfo.setIsHot(Boolean.TRUE);
        shInfoService.update(shInfo);
        return renderResult(Global.TRUE, "启用推荐成功");
    }

    /**
     * 启用房源信息
     */
    @RequiresPermissions("sys:shHouse:edit")
    @RequestMapping(value = "isnothot")
    @ResponseBody
    public String isnothot(ShInfo shInfo) {
        shInfo.setIsHot(Boolean.FALSE);
        shInfoService.update(shInfo);
        return renderResult(Global.TRUE, "取消推荐成功");
    }
}